package vashion.azeroth.back.controller.tran;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import vashion.azeroth.back.controller.base.BaseController;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalInviteCodeManager;
import vashion.azeroth.core.personal.pojo.PersonalInviteCode;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalInviteCodeService;
import vashion.azeroth.core.service.base.BaseService;

@Controller
@RequestMapping("/back/member")
public class BackInviteCodeController<T> extends BaseController {

	@Autowired
	private PersonalInviteCodeService<PersonalInviteCode> personalInviteCodeService;

	@Autowired
	private PersonalInviteCodeManager personalInviteCodeManager;

	/**
	 * 查询所有邀请码接口,不区分状态与过期，未分页
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getAllInviteCode.htm")
	@ResponseBody
	public ApiResult<List<PersonalInviteCode>> getAllInviteCode(HttpServletRequest request) {
		if (null == getLoginUserId(request) || getLoginUserId(request) <= 0) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_LOGIN.getErrorCode(),
					GenericErrorEnum.NOT_LOGIN.getErrorMessage());
		}

		PersonalInviteCode inviteCodeParam = new PersonalInviteCode();

		return personalInviteCodeService.getAllInviteCode(inviteCodeParam);
	}

	/**
	 * 查询所有邀请码接口,不区分状态与过期，未分页
	 * 
	 * @param model
	 * @param member
	 * @param inviteCodeParam
	 * @param memberListPageNum
	 * @param request
	 * @return
	 */
	@RequestMapping("getInviteCodesForSupplier.htm")
	public String getInviteCodesForSupplier(Model model, @ModelAttribute("member") PersonalMember member,
			@ModelAttribute("inviteCode") PersonalInviteCode inviteCodeParam,
			@RequestParam(value = "memberListPageNum", required = false) int memberListPageNum, HttpServletRequest request) {

		if (inviteCodeParam.getPageNum() <= 0) {
			inviteCodeParam.setPageNum(1);
		}
		inviteCodeParam.setFirstRow((inviteCodeParam.getPageNum() - 1) * inviteCodeParam.getPageSize());
		//设置一个干净的查询条件
		PersonalInviteCode inviteCode = new PersonalInviteCode();
		inviteCode.setMemberId(inviteCodeParam.getMemberId());
		inviteCode.setPageNum(inviteCodeParam.getPageNum());
		inviteCode.setFirstRow(inviteCodeParam.getFirstRow());
		inviteCode.setPageSize(inviteCodeParam.getPageSize());
		List<PersonalInviteCode> inviteCodeList = personalInviteCodeManager.find(inviteCode);
		int inviteCodeCount = personalInviteCodeManager.findCnt(inviteCode);
		BaseService page = new BaseService();
		page.initPages(inviteCode, inviteCodeCount);
		model.addAttribute("inviteCodeList", inviteCodeList);
		model.addAttribute("inviteCode", inviteCode);
		model.addAttribute("memberListPageNum", memberListPageNum);
		model.addAttribute("member", member);
		model.addAttribute("page", page);

		return "/member/inviteCodeList";
	}

	/**
	 * @param model
	 * @param inviteCodeParam
	 * @param memberListPageNum
	 * @param request
	 * @return 邀请码显示页面
	 */
	@RequestMapping("generateInviteCode.htm")
	public ModelAndView generateInviteCode(Model model,
			@ModelAttribute("inviteCode") PersonalInviteCode inviteCodeParam, 
			@RequestParam(value = "memberListPageNum", required = false) int memberListPageNum, 
			HttpServletRequest request) {
		if (null == getLoginUserId(request) || getLoginUserId(request) <= 0) {
			return null;
		}
		if (null == inviteCodeParam.getMemberId()) {
			return null;
		}
		personalInviteCodeService.generateInviteCode(inviteCodeParam.getMemberId().toString(), getLoginUserId(request));
		model.addAttribute("msg", "成功添加邀请码。");
		return new ModelAndView("forward:getInviteCodesForSupplier.htm");
	}

	/**
	 * @param model
	 * @param inviteCodeParam
	 * @param memberListPageNum
	 * @param request
	 * @return 邀请码显示页面
	 */
	@RequestMapping("freezeInviteCode.htm")
	public ModelAndView freezeInviteCode(Model model,
			@ModelAttribute("inviteCode") PersonalInviteCode inviteCodeParam, 
			@RequestParam(value = "memberListPageNum", required = false) int memberListPageNum,
			HttpServletRequest request) {
		ApiResult<Boolean> deleteResult = personalInviteCodeService.freezeInviteCode(inviteCodeParam.getInviteCodeId(),
				getLoginUserId(request));

		if (deleteResult.isSuccess() && deleteResult.getData()) {
			return new ModelAndView("forward:getInviteCodesForSupplier.htm");
		} else {
			return null;
		}
	}

	/**
	 * @param model
	 * @param inviteCodeParam
	 * @param memberListPageNum
	 * @param request
	 * @return 邀请码显示页面
	 */
	@RequestMapping("resumeInviteCode.htm")
	public ModelAndView resumeInviteCode(Model model,
			@ModelAttribute("inviteCode") PersonalInviteCode inviteCodeParam, 
			@RequestParam(value = "memberListPageNum", required = false) int memberListPageNum,
			HttpServletRequest request) {
		ApiResult<Boolean> resumeResult = personalInviteCodeService.resumeInviteCode(inviteCodeParam.getInviteCodeId(),
				getLoginUserId(request));

		if (resumeResult.isSuccess() && resumeResult.getData()) {
			return new ModelAndView("forward:getInviteCodesForSupplier.htm");
		} else {
			return null;
		}
	}

}
